package com.es.service.impl;

import com.es.domain.Student;
import com.es.entity.StudentDO;
import com.es.mapper.StudentMapper;
import com.es.service.StudentServiceI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * es-meta-
 *
 * @author Derek
 * @create 2021-11-29 11:16
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentServiceI {

    @Autowired
    private StudentMapper mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(Student student) {
        StudentDO studentDO = new StudentDO();
        BeanUtils.copyProperties(student, studentDO);
        int i = mapper.save(studentDO);
      //  int j = 10/0;
        return i;
    }

    @Override
    public int update(Student student) {
        StudentDO studentDO = new StudentDO();
        BeanUtils.copyProperties(student, studentDO);
        return mapper.update(studentDO);
    }

    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }

    @Override
    public Student get(Integer id) {
        StudentDO studentDO = mapper.get(id);
        Student student = new Student();
        BeanUtils.copyProperties(studentDO,student);
        return student;
    }

    @Override
    public List<Student> list() {
        List<StudentDO> doList = mapper.list();
        List<Student> list = new ArrayList<>();
        doList.forEach(e->{
            Student student = new Student();
            BeanUtils.copyProperties(e, student);
            list.add(student);
        });
        return list;
    }


}
